
import router from "@src/router";

import { getPageConfig } from '@mall/api/homePage'

import { dumpFunc } from '@src/common/dump'
import { getMallConfig } from '@src/api/shop'
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import base from "@src/store/base";
import mall from "@src/store/mall";
Component({

  behaviors: [storeBindingsBehavior],
  storeBindings: [{
      store: base,
      fields: ['themeColor', 'themeTitleColor']
    },{
    store: mall,
    fields: ['curLocation', 'storeId'],
    actions: ['initLocation', 'setAddress', 'setStoreId']
  }],
  data: {
    pageId: null,
    modules: null,
    pageConfig: null,
    name: null,
    login: false,
  },
  methods: {
    onLoad(option) {
      let {pageId, storeId} = option;
      if (storeId) {
        this.setStoreId(storeId);
      }
      this.setData({ pageId: pageId})
    },
    onShareAppMessage() {
      if (!this.data.storeId) {
        return {
          title: this.data.businessName,
          path: `pages/index/main`,
        };
      }
      let pageConfig = this.data.pageConfig;
      let data = { path: `sp-mall/pages/active-page/index?pageId=`+ this.data.pageId + "&storeId=" + this.data.storeId,}
      if (pageConfig && pageConfig.shareTitle) {
        data['title'] = pageConfig.shareTitle;
      } else {
        data['title'] = this.data.businessName;
      }
      if (pageConfig && pageConfig.shareImageUrl) {
        data['imageUrl'] = pageConfig.shareImageUrl;
      }
      return data;
    },
    onLogin() {
      this.updateStoreBindings();
      this.initMoudle();
      this.setData({ login: true})
    },
    //加载活动页组件
    initMoudle() {
      let params = { pageId: this.data.pageId }
      getPageConfig(params).then(res => { if (res.code == 0) { this.setData({
        modules: res.data.modules,
        name: res.data.config.name,
        pageConfig: res.data.config
      }) } })
    },
    jumpPage(e) {
      let dump = e.currentTarget.dataset.item.dump
      dumpFunc(dump);
    },
    dumpPage(e) {
      let item = e.currentTarget.dataset.item;
      console.log('e', e)
      if (item && item[0].dump) {
        dumpFunc(item[0].dump)
      }
    },
    async onPullDownRefresh() {
      await this.initMoudle();
      wx.stopPullDownRefresh();
    },
    handleLogin() {
      const loginComp = this.selectComponent("#login");
      loginComp.requireLogin();
    },
  }
})
